<template>
  <div>
    <div class="collect-table-list">
      <div class="collect-table-item">
        <div class="label">汇总名称</div>
        <div class="input-container">
          <el-input v-model="collectColumnTitle" maxlength="20" type="text" placeholder="请输入汇总名称"/>
        </div>
      </div>
      <div v-for="(item,index) in collectColumnField" :key="index" class="collect-table-item">
        <div class="label">{{ item.columnName }}</div>
        <div class="input-container">
          <el-select v-model="item.type" placeholder="请选择">
            <el-option
              v-for="item in typeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    fieldList: {
      type: Array,
      default: () => {
        return [];
      }
    }
  },
  data() {
    return {
      collectColumnTitle: '',
      collectColumnField: [],
      typeOptions: [
        { id: 1, name: '求和' },
        { id: 0, name: '平均' }
      ]
    };
  },
  watch: {
    fieldList: {
      handler(newVal, oldVal) {
        this.handlerField(newVal, oldVal);
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    setDefault(data) {
      this.collectColumnTitle = data.collectColumn.title;
      this.collectColumnField = data.collectColumn.field;
    },
    handlerField(newVal, oldVal) {
      const n = this.fieldList.length;
      const clen = this.collectColumnField.length;
      const len = n - clen;
      if (len >= 0) {
        for (let i = 0; i < n; i++) {
          const columnName = this.fieldList[i].columnName;
          if (this.collectColumnField[i] && this.collectColumnField[i].columnName) {
            this.collectColumnField[i].columnName = columnName;
          } else {
            this.$set(this.collectColumnField, i, {
              columnName,
              type: 1 // 0:平均 1:求和
            });
          }
        }
      } else {
        let deleteIndex = -1;
        if (oldVal) {
          oldVal.forEach((i, index) => {
            if (newVal[index] && newVal[index].columnName !== i.columnName) {
              deleteIndex = index;
            }
          });
        }
        this.collectColumnField.splice(deleteIndex === -1 ? n : deleteIndex, clen - n);
      }
    },
    getParams() {
      let error = '';
      if (!this.collectColumnTitle) {
        error = '请输入汇总行名称';
      }
      return {
        error,
        data: {
          title: this.collectColumnTitle,
          field: this.collectColumnField
        }
      };
    }
  }
};
</script>

<style lang="scss" scoped>
.collect-table-list {
  border: 1px solid #ddd;
  overflow: hidden;
  display: flex;
  margin-top: 10px;
  border-radius: 4px;
  .collect-table-item {
    border-right: 1px solid #ddd;
    width: 120px;
    flex:1;
    &:last-child {
      border-right: none;
    }
  }
  .label {
    font-size: 14px;
    color: #333;
    line-height: 32px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border-bottom: 1px solid #ddd;
    padding: 5px 10px;
  }
  .input-container {
    padding: 5px 10px;
    &::v-deep {
      .el-input {
        width: 100%;
      }
      .el-input__inner {
        border: none;
        font-size: 14px;
        height: 32px;
        padding: 0;
        width: 100% !important;
      }
    }
  }
}
</style>
